<?php
/**
 * @Notes: 文件描述
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2021/4/19 15:28
 */

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\console\input\Option;
use think\Db;
use think\facade\Hook;
use think\facade\Log;
use QL\QueryList;
use QL\Ext\CurlMulti;

class DeadLink extends Command
{

    protected function configure()
    {
        $this->setName('dead:link')
            ->setDescription('回收官网收录死链检测');
    }

    protected function execute(Input $input, Output $output)
    {
        ini_set('memory_limit', '512M');

        $output->writeln("=========date:" . date('Y-m-d H:i:s') . "===============");

        //$list = QueryList::get("https://www.baidu.com/s?wd=site:www.huanhuanhuishou.com")->find('.xpath-log')->attrs('mu');
        //var_dump($list);die;

        for ($i = 0; $i <= 75; $i++) {
            $output->writeln("=========开始抓取第 " . ($i + 1) . " 页===============");

            $flag = $i * 10;
            $url = "https://www.baidu.com/s?wd=site:www.huanhuanhuishou.com&pn=".$flag."&oq=site:www.huanhuanhuishou.com";
            echo $url;
            sleep(2);

            $list = QueryList::get($url)->find('#content_left')->find('.result')->attrs('mu');

            $arr = [];
            foreach ($list as $item) {
                $status = $this->checkUrlStatus($item);
                //$arr[] = [
                //    "url" => $item,
                //    "url_md5" => md5($item),
                //    "code" => $status,
                //    "create_time" => time()
                //];
                if ($status != 200) {
                    $arr[] = [
                        "url" => $item,
                        "url_md5" => md5($item),
                        "code" => $status,
                        "create_time" => time()
                    ];
                }
            }

            var_dump($arr);

            if ($arr) {
                $res = Db::name('link')->insertAll($arr);

                if ($res) {
                    $output->writeln("===========检测到" . count($arr) . "条死链，已入库=============");
                } else {
                    $output->writeln("===========数据写入失败=============");
                }
            }


        }

        $output->writeln("============date:" . date("Y-m-d H:i:s") . "执行完毕==============");
    }

// 扫描目录下所有文件
    protected
    function scanFile($path)
    {
        $result = [];
        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                if (is_dir($path . '/' . $file)) {
                    $this->scanFile($path . '/' . $file);
                } else {
                    $result[] = basename($file);
                }
            }
        }
        return $result;
    }

    protected
    function checkUrlStatus($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200
        curl_close($ch);

        return $status;
    }

}